package com.surfilter.msmp.common.util;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumFormatUtils {

    private static String regexNum = "[0-9]+";

    private static String regexNumSplit = "[0-9]+(\\.)?";

    private static String regexChinese = "[\u4e00-\u9fa5]+";

    private static String regexSplit = "((,|\\s|，)*[0-9]+)+";

    private static String regexSpli1t = "[0-9]+";

    private static String regexSpli2t = "[0-9]+(,)?[0-9]+((,)?[0-9]+)*";

    private static String regexSpli3t = "([0-9]+(,)?)*(位|个|人|元)";

    private static String regexBiliW = ".*亿.*万.*";

    private static String regexB = ".*亿.*";

    private static String regexWY = ".*万.+";

    private static String regexW = ".*万";

    public static long parseNum(String text) {
        long result = 0;
        try {
            //判断是否含有中文
            String regB = "([0-9]+(,|\\.|\\s|，)*)*亿";
            String regW = "([0-9]+(,|\\.|\\s|，)*)*万";
            String regY = "万([0-9]+(,|\\.|\\s|，)*)*";
            if (existString(text, regexChinese)) {
                if (existString(text, regexBiliW)) {
                    result += parseStringNumToLongChen(text, regB, 100000000);
                    result += parseStringNumToLongChen(text, regW, 10000);
                    result += parseStringNumToLongChen(text, regY, 1);
                } else if (existString(text, regexB)) {
                    result += parseStringNumToLongChen(text, regB, 100000000);
                } else if (existString(text, regexWY)) {
                    result += parseStringNumToLongChen(text, regW, 10000);
                    result += parseStringNumToLongChen(text, regY, 1);
                } else if (existString(text, regexW)) {
                    result += parseStringNumToLongChen(text, regW, 10000);
                } else {
                    if (existString(text, regexSpli2t)) {
                        result += parseStringNumToLongChen(text, regexSpli2t, 1);
                    } else if (existString(text, regexSpli3t)) {
                        result += parseStringNumToLongChen(text, regexSpli3t, 1);
                    } else {
                        result = parseStringNumToLong(text);
                    }
                }
            } else {
                result = parseStringNumToLong(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判断字符串中是否含有匹配项
     *
     * @param text
     * @return
     */
    public static boolean existString(String text, String regex) {
        if (text == null || text.trim().equalsIgnoreCase("")) return false;
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(text);
        return match.find();
    }

    private static long parseStringNumToLongChen(String text, String regex, long cheng) {

        long result = 0;
        text = parseString(text, regex, false);
        if (text == null || text.trim().equalsIgnoreCase("")) return 0;
        text = parseString(text, regexNumSplit, true);
        if (text == null || text.trim().equalsIgnoreCase("")) return 0;
        Double doub = Double.parseDouble(text) * cheng;
        if (doub > Long.MAX_VALUE) {
            result = 0;
        } else {
            result = doub.longValue();
        }
        return result;
    }

    /**
     * @param text     需要匹配的文本
     * @param regex    正则表达
     * @param regexAll 是否匹配所有
     * @return 返回匹配到的字符串
     */
    public static String parseString(String text, String regex, boolean regexAll) {
        StringBuffer strb = new StringBuffer();
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(text);
        while (match.find()) {
            strb.append(match.group());
            if (!regexAll) break;
        }
        return strb.toString();
    }

    /**
     * 不含中文的数字解析
     *
     * @param text
     * @return
     */
    public static long parseStringNumToLong(String text) {
        long result = 0;
        try {
            text = parseString(text, regexSplit, false);
            if (text == null || text.trim().equalsIgnoreCase("")) return result;
            StringBuffer strb = new StringBuffer();
            Pattern patt = Pattern.compile(regexNum);
            Matcher match = patt.matcher(text);
            while (match.find()) {
                strb.append(match.group());
            }
            String str = strb.toString();
            if (StringUtils.isNumeric(str)) {
                result = Long.parseLong(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static long noParseStringNumToLong(String text) {
        long result = 0;
        try {
            text = parseString(text, regexSpli1t, true);
            if (text == null || text.trim().equalsIgnoreCase("")) return result;
            StringBuffer strb = new StringBuffer();
            Pattern patt = Pattern.compile(regexNum);
            Matcher match = patt.matcher(text);
            while (match.find()) {
                strb.append(match.group());
            }
            String str = strb.toString();
            if (StringUtils.isNumeric(str)) {
                result = Long.parseLong(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) throws IOException {
//		List<String> results = FileUtils.readLines(new File("C:\\Users\\xubin\\workspace\\funds_taskt\\yhzl"), "utf-8");
//		for(String result : results){
//			System.out.println(parseNum(result) + "---" + result);
//		}
        String mats = "￥1482亿1294万";
        System.out.println(parseNum(mats) + "---" + mats);
//		System.out.println(Long.parseLong(mats));
//		System.out.println(StringUtils.isNumeric("123.09"));

    }

}
